/*
* Copyright 2015 2dfire.com All right reserved. This software is the
* confidential and proprietary information of 2dfire.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with 2dfire.com.
*/
package com.twodfire.merchant.poi;

import java.beans.IntrospectionException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author guipi
 * @description
 * @date 2017/7/8
 * ----------重大变更记录------------
 */
public class WriteConfig extends AbstractConfig {

    private Object content;
    private Collection arrayContent;
    private Map<String,Method> objectMethodMap = new HashMap<>();
    private Map<String,Method> arrayMethodMap = new HashMap<>();

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) throws IntrospectionException {
        this.content = content;
        //如果没设置key，则以对象的属性为key
        if(objectKeys==null){
            objectKeys = new HashSet<>();
        }
        createKeys(content,objectKeys,objectMethodMap);
    }

    public Collection<Object> getArrayContent() {
        return arrayContent;
    }

    public void setArrayContent(Collection arrayContent) throws IntrospectionException {
        this.arrayContent = arrayContent;
        if(arrayKeys ==null){
            arrayKeys = new HashSet<>();
        }
        createKeys(arrayContent.iterator().next(),arrayKeys,arrayMethodMap);
    }

    public Map<String, Method> getObjectMethodMap() {
        return objectMethodMap;
    }

    public Map<String, Method> getArrayMethodMap() {
        return arrayMethodMap;
    }

    private void createKeys(Object object, Collection<String> keys, Map<String, Method> methodMap) throws IntrospectionException {
        if(keys.isEmpty()) {
            if(object instanceof Map){
                for(Object key:((Map) object).keySet()){
                    if(key!=null){
                        keys.add(key.toString());
                    }
                }
            }else{
                generatorKeyAndMethod(object.getClass(),keys, methodMap,"get");
            }
        }else if(methodMap.isEmpty()){
            if(!(object instanceof Map)){
                generatorMethod(object.getClass(),keys, methodMap,"get");
            }
        }
    }
}
